home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8271 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: callback function in a class
  5. Date: 16 Feb 1996 11:16:20 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4g1p24$lpi@news1.usa.pipeline.com>
  8. References: <Pine.OSF.3.91.960214160359.19924A-100000@stio1>
  9. NNTP-Posting-Host: pipe4.h1.usa.pipeline.com
  10. X-PipeUser: grantp
  11. X-PipeHub: usa.pipeline.com
  12. X-PipeGCOS: (Pete)
  13. X-Newsreader: Pipeline USA v3.3.0
  14.  
  15. On Feb 14, 1996 16:09:30 in article <callback function in a class>,
  16. 'Schmidt Carsten <i010@stio1>' wrote: 
  17.  
  18.  
  19. >Hi, everybody, 
  20. >Does anybody have an idea, how I can put a callback function in a class,  
  21. >look for the example: 
  22. >class X {  
  23. >DWORD ThreadProc (..parms...); 
  24. >void StartIt (void); 
  25. >.... 
  26. >}; 
  27. >DWORD X::ThreadProc (...parms...) 
  28. >{ 
  29. >.... 
  30. >} 
  31. >void X::StartIt (void) 
  32. >{ 
  33. >hThread = CreateThread (..., ThreadProc, ...); 
  34. >} 
  35. >Don't worry about Threads, it is just an example. I try to find a  
  36. >solution for the general problem: How to use a callback-function in a
  37. class. 
  38. There's no way to implement a callback function as a nonstatic 
  39. C++ member directly.  It is, however, easy to do it indirectly. 
  40.  
  41. void callback (void * p) 
  42.  { 
  43.    ((X*)p)->ThreadProc(); 
  44.  } 
  45.  
  46. then you can: 
  47. hThread = CreateThread (..., callback, (void*)this, ...); 
  48.  
  49. Of course, you are now responsible for ensuring that the pointer 
  50. passed to callback is compatible with the type to which it's 
  51. being casted.  Also, if you like, callback canb e implemented 
  52. as a static member function of X.  
  53.  
  54.  
  55. -- 
  56. Pete Grant 
  57. Kalevi, Inc. 
  58. Software Engineering & development
  59.